﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace TextEditApp.Algorithm
{
    class SimpleDictionary
    {
        private Trie trie;
        private static SimpleDictionary dictionary = new SimpleDictionary();
        private SimpleDictionary()
        {

            this.trie = new Trie();
            try
            {
                // 创建一个 StreamReader 的实例来读取文件 
                // using 语句也能关闭 StreamReader
                using (StreamReader sr = new StreamReader("../../Algorithm/Txt/simple_dictionary.txt"))
                {
                    string line;

                    // 从文件读取并显示行，直到文件的末尾 
                    while ((line = sr.ReadLine()) != null)
                    {
                        
                        this.trie.Insert(line.ToLower());
                    }
                }
            }
            catch (Exception e)
            {
                // 向用户显示出错消息
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
        }

        /// <summary>
        /// 返回词典
        /// </summary>
        public static SimpleDictionary GetDictionary
        {
            get
            {
                return dictionary;
            }
        }

        /// <summary>
        /// 判读是否是一个英文单词
        /// </summary>
        /// <param name="word"></param>
        /// <returns></returns>
        public bool isEnglish(string word)
        {
            if (this.trie.Search(word.ToLower())) return true;
            return false;
        }
    }
}
